# MovieScreen - This screen allows user to view movie information
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import os
import gtk
import pango
import string
import clutter

from frontend.gui.screens.screen import Screen
from frontend.gui.transitions.transition import Transition

# Widgets
from frontend.gui.widgets.text_menu import TextMenu
from frontend.gui.widgets.scroll_area import ScrollArea
from frontend.gui.widgets.text_menu_item import TextMenuItem

# Video library
from frontend.medialibrary.videos import Movie
from frontend.medialibrary.videos import TVSeries
from frontend.medialibrary.videos import VideoClip
from frontend.medialibrary.videos import TVEpisode
from frontend.medialibrary.videos import VideoLibrary

class MovieScreen(Screen):
    """
    MovieScreen
    
    This screen contains information of one Movie.
    """
    
    def __init__(self, ui, frontend, movie):
        """
        Initialize screen
        
        @param frontend: FrontendClient object
        @param theme: Theme object
        @param movie: Movie object
        """
        self.ui = ui
        self.frontend = frontend
        self.theme = frontend.getTheme()
        self.group = clutter.Group()
        self.movie = movie
        
        # Screen Title (Displayed at the bottom left corner)
        self.screen_title = clutter.Label()
        self.screen_title.set_font_name(self.theme.getFont() + ' 100px')
        self.screen_title.set_color(self.theme.getColor("screentitle"))
        self.screen_title.set_text("Movie")
        self.screen_title.set_name("screen_title")
        (screen_title_width, screen_title_height) = self.screen_title.get_size()
        screen_title_x = 0
        screen_title_y = self.ui.STAGE_HEIGHT - screen_title_height + 20
        self.screen_title.set_position(screen_title_x, screen_title_y)
        self.screen_title.show()
        self.group.add(self.screen_title)
        
        self.create_menu()
        self.create_movie_information()
                
        self.group.show()
        self.ui.getStage().add(self.group)
        
    def create_movie_information(self):
        # Movie art texture
        if self.movie.has_cover_art():
            pixbuf = gtk.gdk.pixbuf_new_from_file(self.movie.get_cover_art_url())
        else:
            pixbuf = gtk.gdk.pixbuf_new_from_file(self.theme.getImage("default_movie_art"))
        movie_art = clutter.Texture()
        movie_art.set_pixbuf(pixbuf)
        movie_art.set_position(self.ui.get_abs_x_pos(0.33), self.ui.get_abs_y_pos(0.1))
        movie_art.set_size(135,208)
        movie_art.show()
        self.group.add(movie_art)
        
        # Movie title
        title = clutter.Label()
        title.set_text(self.movie.get_title())
        title.set_font_name(self.theme.getFont() + ' bold 32px')
        title.set_color(self.theme.getColor("title"))
        title.set_line_wrap(False)
        title.set_ellipsize(pango.ELLIPSIZE_END)
        title.set_position(self.ui.get_abs_x_pos(0.47), self.ui.get_abs_y_pos(0.1))
        title.set_size(700,40)
        title.show()
        self.group.add(title)
        
        # Movie release year
        year = clutter.Label()
        year.set_text("Released in " + self.movie.get_year())
        year.set_font_name(self.theme.getFont() + ' 28px')
        year.set_color(self.theme.getColor("subtitle"))
        year.set_line_wrap(False)
        year.set_ellipsize(pango.ELLIPSIZE_END)
        year.set_position(self.ui.get_abs_x_pos(0.47), self.ui.get_abs_y_pos(0.30))
        year.set_size(700,40)
        year.show()
        self.group.add(year)
        
        # Show only 2 genres (or one if there is only one)
        genres = self.movie.get_genres()
        if len(genres) > 1:
            genre = genres[0] + "/" + genres[1]
        else:
            genre = genres[0]
        
        # Runtime and genres
        info = clutter.Label()
        info.set_text(str(self.movie.get_runtime()) + "min, " + genre)
        info.set_font_name(self.theme.getFont() + ' 28px')
        info.set_color(self.theme.getColor("subtitle"))
        info.set_line_wrap(False)
        info.set_ellipsize(pango.ELLIPSIZE_END)
        info.set_position(self.ui.get_abs_x_pos(0.47), self.ui.get_abs_y_pos(0.24))
        info.set_size(700,40)
        info.show()
        self.group.add(info)
        
        # Stars (rating)
        star_pixbuf = gtk.gdk.pixbuf_new_from_file(self.theme.getImage("star"))
        star = clutter.Texture()
        star.set_pixbuf(star_pixbuf)
        star2_pixbuf = gtk.gdk.pixbuf_new_from_file(self.theme.getImage("star2"))
        star2 = clutter.Texture()
        star2.set_pixbuf(star2_pixbuf)

        for i in range(self.movie.get_rating()):
            tex = clutter.CloneTexture(star)
            tex.set_position(self.ui.get_abs_x_pos(0.47) + (50 * i), self.ui.get_abs_y_pos(0.17))
            tex.set_size(40,40)
            tex.show()
            self.group.add(tex)
        
        dark_star = 5 - self.movie.get_rating()
        for i in range(dark_star):
            tex = clutter.CloneTexture(star2)
            tex.set_position(self.ui.get_abs_x_pos(0.47) + (50 * (i+ self.movie.get_rating())), self.ui.get_abs_y_pos(0.17))
            tex.set_size(40,40)
            tex.show()
            self.group.add(tex)
            
            
        # Plot
        plot = clutter.Label()
        plot.set_font_name(self.theme.getFont() + ' 28px')
        plot.set_color(self.theme.getColor("subtitle"))
        plot.set_text(self.movie.get_plot())
        plot.set_justify(True)
        plot.set_line_wrap_mode(pango.WRAP_WORD)
        plot.set_line_wrap(True)
        plot.set_width(700)
        self.scroll_area = ScrollArea(700,270, self.theme)
        self.scroll_area.set_step_size(30)
        self.scroll_area.set_content(plot)
        self.scroll_area.set_position(self.ui.get_abs_x_pos(0.33), self.ui.get_abs_y_pos(0.38))
        self.scroll_area.show()
        self.group.add(self.scroll_area)
        
        # Actors
        act_title = clutter.Label()
        act_title.set_text("Starring")
        act_title.set_font_name(self.theme.getFont() + ' 28px')
        act_title.set_color(self.theme.getColor("title"))
        act_title.set_line_wrap(False)
        act_title.set_ellipsize(pango.ELLIPSIZE_END)
        act_title.set_position(self.ui.get_abs_x_pos(0.33), self.ui.get_abs_y_pos(0.8))
        act_title.show()
        self.group.add(act_title)
        
        actors = clutter.Label()
        actors.set_text(string.join(self.movie.get_actors()[:3], ", "))
        actors.set_font_name(self.theme.getFont() + ' 28px')
        actors.set_color(self.theme.getColor("subtitle"))
        actors.set_line_wrap(False)
        actors.set_ellipsize(pango.ELLIPSIZE_END)
        actors.set_position(self.ui.get_abs_x_pos(0.46), self.ui.get_abs_y_pos(0.8))
        actors.set_size(700,40)
        actors.show()
        self.group.add(actors)
        
        # Directors
        dict_title = clutter.Label()
        dict_title.set_text("Directed by")
        dict_title.set_font_name(self.theme.getFont() + ' 28px')
        dict_title.set_color(self.theme.getColor("title"))
        dict_title.set_line_wrap(False)
        dict_title.set_ellipsize(pango.ELLIPSIZE_END)
        dict_title.set_position(self.ui.get_abs_x_pos(0.33), self.ui.get_abs_y_pos(0.86))
        dict_title.show()
        self.group.add(dict_title)
        
        dlist = self.movie.get_directors()
        if len(dlist) > 1:
            director = dlist[0] + ", " + dlist[1]
        else:
            director = dlist[0]
        directors = clutter.Label()
        directors.set_text(director)
        directors.set_font_name(self.theme.getFont() + ' 28px')
        directors.set_color(self.theme.getColor("subtitle"))
        directors.set_line_wrap(False)
        directors.set_ellipsize(pango.ELLIPSIZE_END)
        directors.set_position(self.ui.get_abs_x_pos(0.46), self.ui.get_abs_y_pos(0.86))
        directors.set_size(700,40)
        directors.show()
        self.group.add(directors)
        
        # Writers
        wri_title = clutter.Label()
        wri_title.set_text("Written by")
        wri_title.set_font_name(self.theme.getFont() + ' 28px')
        wri_title.set_color(self.theme.getColor("title"))
        wri_title.set_line_wrap(False)
        wri_title.set_ellipsize(pango.ELLIPSIZE_END)
        wri_title.set_position(self.ui.get_abs_x_pos(0.33), self.ui.get_abs_y_pos(0.92))
        wri_title.show()
        self.group.add(wri_title)
        
        wlist = self.movie.get_writers()
        if len(wlist) > 1:
            writer = wlist[0] + ", " + wlist[1]
        else:
            writer = wlist[0]
        writers = clutter.Label()
        writers.set_text(writer)
        writers.set_font_name(self.theme.getFont() + ' 28px')
        writers.set_color(self.theme.getColor("subtitle"))
        writers.set_line_wrap(False)
        writers.set_ellipsize(pango.ELLIPSIZE_END)
        writers.set_position(self.ui.get_abs_x_pos(0.46), self.ui.get_abs_y_pos(0.92))
        writers.set_size(700,40)
        writers.show()
        self.group.add(writers)
    
    def create_menu(self):
        """
        Create menu of the screen. This displayed the left side of the screen.
        """
        self.menu = TextMenu(self.theme, 
                             self.frontend.get_configuration().show_effects())
        self.menu.set_row_count(1)
        #self.track_menu.set_visible_column_count(10)
        self.menu.set_item_size(300, 60)
        self.menu.set_position(self.ui.get_abs_x_pos(0.07), self.ui.get_abs_y_pos(0.1))
        
        item1 = TextMenuItem(300, 60, self.theme, "Watch")
        item1.set_userdata("watch")
        self.menu.add_actor(item1)
        
        item2 = TextMenuItem(300, 60, self.theme, "Remove")
        item2.set_userdata("remove")
        self.menu.add_actor(item2)
                
        self.menu.show()
        self.menu.set_active(True)
        self.group.add(self.menu)
        
    def remove_movie_callback(self):
        """
        Remove movie from movie cache and harddrive.
        This method is called from QuestionDialog if user selects YES.
        """
        self.movie.remove()
    
    def get_group(self):
        """
        Get all actors of the screen.
        @return clutter.Group 
        """
        return self.group

    def get_type(self):
        """Return screen type."""
        return Screen.NORMAL
    
    def get_name(self):
        """Return screen name (human readble)"""
        return "MovieScreen"
    
    def handle_key_press_event(self, event):   
        """ Handle key pressed event."""
    
        #UP KEY
        if event.keyval == clutter.keysyms.Up:
            if self.menu.is_active():
                self.menu.move(self.menu.UP)
            else:
                self.scroll_area.scroll_up()
        
        #DOWN KEY  
        elif event.keyval == clutter.keysyms.Down:
            if self.menu.is_active():
                self.menu.move(self.menu.DOWN)
            else:
                self.scroll_area.scroll_down()
        
        #HOME KEY
        elif event.keyval == clutter.keysyms.Home:
            self.scroll_area.scroll_to_top()
        
        #END KEY
        elif event.keyval == clutter.keysyms.End:
            self.scroll_area.scroll_to_bottom()
        
        #PAGE UP
        elif event.keyval == clutter.keysyms.Page_Up:
            self.scroll_area.scroll_page_up()
        
        #PAGE DOWN
        elif event.keyval == clutter.keysyms.Page_Down:
            self.scroll_area.scroll_page_down()
            
        #LEFT KEY
        elif event.keyval == clutter.keysyms.Left:
            self.menu.set_active(True)
            self.scroll_area.set_active(False)
        
        # RIGHT KEY
        elif event.keyval == clutter.keysyms.Right:
            self.menu.set_active(False)
            self.scroll_area.set_active(True)
        
        # RETURN KEY
        elif event.keyval == clutter.keysyms.Return:
            if self.menu.is_active():
                item = self.menu.get_current_menuitem().get_userdata()
                if item == "watch":
                    mp = self.ui.get_media_player()
                    mp.set_media(self.movie)
                    mp.play()
                    self.ui.changeScreen(self.ui.createScreen("video_osd"), Transition.FORWARD)
                elif item == "remove":
                    self.ui.changeScreen(self.ui.createScreen("question_dialog", [self, self.remove_movie_callback, "Are you sure you want to remove this movie?"]), Transition.FORWARD)